home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / WANDR330.ZIP / SRC / MONSTERS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-26  |  6.4 KB  |  233 lines

  1. #include "wand_head.h"
  2.  
  3. typedef struct { int d[2]; } direction;
  4.  
  5. #ifdef    LINT_ARGS    /* M001 */
  6. direction new_direction(int, int, int, int);
  7. #else
  8. direction new_direction();
  9. #endif
  10.  
  11. extern int debug_disp;
  12. extern char screen[NOOFROWS][ROWLEN+1];
  13. extern WINDOW *win;
  14.  
  15. /* Add a spirit to the chain */
  16. /* Maintain a doubly linked list to make reuse possible.
  17.    tail_of_list is *NOT* the last monster allocated, but
  18.    the last monster alloted to a screen.  start_of_list
  19.    is a dummy entry to ease processing. last_of_list
  20.    is the last entry allocated. */
  21.  
  22. extern struct mon_rec *last_of_list, *tail_of_list;
  23. extern struct mon_rec start_of_list;
  24.  
  25. struct mon_rec *make_monster(x, y)
  26. int x, y;
  27. {
  28.     char *malloc();
  29. #define MALLOC (struct mon_rec *)malloc(sizeof(struct mon_rec))
  30.     struct mon_rec *monster;
  31.     if (tail_of_list->next == NULL) {
  32.     if ((last_of_list = MALLOC) == NULL)
  33.         return NULL;
  34.     tail_of_list->next = last_of_list;
  35.     last_of_list->prev = tail_of_list;
  36.     last_of_list->next = NULL;
  37.     }
  38.     monster = tail_of_list = tail_of_list->next;
  39.     monster->x = x;
  40.     monster->y = y;
  41.     monster->mx = 1;    /* always start moving RIGHT. (fix later)  */
  42.     monster->my = 0;
  43.     monster->under = ' ';
  44.     return monster;
  45. }
  46.  
  47. /* 'follow lefthand wall' algorithm for baby monsters */
  48.  
  49. direction new_direction(x, y, bx, by)
  50. int x, y, bx, by;
  51. {
  52.     direction out;
  53.  
  54.     if (viable((x+by),(y-bx))) {
  55.     out.d[0] = by;
  56.     out.d[1] = -bx;
  57.     return out;
  58.     }
  59.     if (viable((x+bx),(y+by))) {
  60.     out.d[0] = bx;
  61.     out.d[1] = by;
  62.     return out;
  63.     }
  64.     if (viable((x-by),(y+bx))) {
  65.     out.d[0] = -by;
  66.     out.d[1] = bx;
  67.     return out;
  68.     }
  69.     if (viable((x-bx),(y-by))) {
  70.     out.d[0] = -bx;
  71.     out.d[1] = -by;
  72.     return out;
  73.     }
  74.     out.d[0] = -bx;
  75.     out.d[1] = -by;
  76.     return out;
  77. }
  78.  
  79. int move_monsters(mxp, myp, score, howdead, sx, sy, nf, bell, x, y, diamonds)
  80. int *mxp, *myp, sx, sy, nf, bell, x, y, diamonds;
  81. long *score;
  82. char *howdead;
  83. {
  84.     int xdirection, ydirection, hd, vd;
  85.     int deadyet = 0;
  86.     int bx, by, nbx, nby, tmpx,tmpy;
  87.     direction new_disp;
  88.     struct mon_rec *monster,*current;
  89.     char buffer[25];
  90.  
  91. /* big monster first */
  92.     if (*mxp == -2) {        /* has the monster been killed ? */
  93.     *score+=100;
  94.     *mxp = *myp = -1;
  95.     move(3,48);
  96.     sprintf(buffer,"%ld\t %d\t",*score,nf);
  97.     (void) addstr(buffer);
  98.     draw_symbol(50,11,' ');
  99.     move(12,56); addstr("              ");
  100.     move(13,56); addstr("              ");
  101.     move(16,0);
  102.     wrefresh(win);
  103.     }            /* if monster still alive */
  104.     if (*mxp != -1) {    /* then move that monster ! */
  105.     screen[*myp][*mxp] = ' ';
  106.     if (*mxp > x)
  107.         xdirection = -1;
  108.     else
  109.         xdirection = 1;
  110.     if (!debug_disp) {
  111.         if ((*myp < (sy+4)) && (*myp > (sy-4)) && (*mxp < (sx+6)) &&
  112.         (*mxp > (sx-6)))
  113.         draw_symbol((*mxp-sx+5)*3,(*myp-sy+3)*2,' ');
  114.     } else {
  115.         draw_object(*myp+1,*mxp+1,' ');
  116.     }
  117.     if ((hd = (*mxp-x))<0)
  118.         hd = -hd;
  119.     if ((vd = (*myp-y))<0)
  120.         vd = -vd;
  121.     if ((hd > vd) && ((*mxp+xdirection) < ROWLEN) &&
  122.         ((screen[*myp][*mxp+xdirection] == ' ') ||
  123.         (screen[*myp][*mxp+xdirection] == '@')))
  124.         *mxp += xdirection;
  125.     else {
  126.         if (*myp > y)
  127.         ydirection = -1;
  128.         else
  129.         ydirection = 1;
  130.         if (((*myp+ydirection) < NOOFROWS) &&
  131.         ((screen[*myp+ydirection][*mxp] == ' ') ||
  132.         (screen[*myp+ydirection][*mxp] == '@')))
  133.         *myp += ydirection;
  134.         else if (((*mxp+xdirection) < ROWLEN) &&
  135.         (screen[*myp][*mxp+xdirection] == ' ') ||
  136.         (screen[*myp][*mxp+xdirection] == '@'))
  137.         *mxp += xdirection;
  138.     }
  139.     if (!debug_disp) {
  140.         if ((*myp < (sy+4)) && (*myp > (sy-4)) &&
  141.         (*mxp < (sx+6)) && (*mxp > (sx-6)))
  142.         draw_symbol((*mxp-sx+5)*3,(*myp-sy+3)*2,'M');
  143.     } else {
  144.         draw_object(*myp+1,*mxp+1,'M');
  145.     }
  146.     if (screen[*myp][*mxp] == '@') {    /* ha! gottim! */
  147.         strcpy(howdead,"a hungry monster");
  148.         move(16,0);
  149.         wrefresh(win);
  150.         deadyet = 1;
  151.     }
  152.     screen[*myp][*mxp] = 'M';
  153.     move(16,0);
  154.     wrefresh(win);
  155.     }
  156.  
  157.     current = &start_of_list;        /* baby monsters now */
  158.     while ((current != tail_of_list) && (!deadyet)) {
  159.     /* deal with those little monsters */
  160.     monster = current->next;
  161.     new_disp = new_direction(monster->x, monster->y, monster->mx, monster->my);
  162.     if (monster->under != 'S') {    /* if on top of another baby */
  163.         screen[monster->y][monster->x] = monster->under;
  164.         if (!debug_disp) {
  165.         if ((monster->y < (sy+4)) && (monster->y > (sy-4)) &&
  166.             (monster->x < (sx+6)) && (monster->x > (sx-6)))
  167.             draw_symbol((monster->x-sx+5)*3,(monster->y-sy+3)*2,monster->under);
  168.         } else {
  169.         draw_object(monster->y+1,monster->x+1,monster->under);
  170.         }
  171.         if (monster->under == ' ')
  172.         deadyet += check(&*mxp,&*myp,monster->x,monster->y,new_disp.d[0],new_disp.d[1],sx,sy,howdead);
  173.     } else
  174.         monster->under = ' ';
  175.     monster->mx = new_disp.d[0];
  176.     monster->my = new_disp.d[1];
  177.     monster->x += monster->mx;
  178.     monster->y += monster->my;
  179.     monster->under = screen[monster->y][monster->x];
  180.     screen[monster->y][monster->x] = 'S';    /* move into new space */
  181.     if (!debug_disp) {
  182.         if ((monster->y < (sy+4)) && (monster->y > (sy-4)) &&
  183.         (monster->x < (sx+6)) && (monster->x > (sx-6)))
  184.         draw_symbol((monster->x-sx+5)*3,(monster->y-sy+3)*2,'S');
  185.     } else {
  186.         draw_object(monster->y+1,monster->x+1,'S');
  187.     }
  188.     if (monster->under == '@') {    /* monster hit you? */
  189.         strcpy(howdead,"the little monsters");
  190.         move(16,0);
  191.         wrefresh(win);
  192.         deadyet = 1;
  193.         monster->under = ' ';
  194.     }
  195.     if (monster->under == '+') {    /* monster hit cage? */
  196. #ifdef NOISY
  197.         if (bell) printf("\007");
  198. #endif
  199.         *score += 20;
  200.         move(3,48);
  201.         sprintf(buffer,"%ld\t %d\t %d ",*score,nf,diamonds);
  202.         (void) addstr(buffer);
  203.         /* remove from chain, and insert at the end (at last_of_list) */
  204.         if (monster == tail_of_list)
  205.         tail_of_list = tail_of_list->prev;
  206.         else {
  207.         current->next = monster-> next;
  208.         current->next->prev = current;
  209.         monster->next = NULL;
  210.         monster->prev = last_of_list;
  211.         last_of_list->next = monster;
  212.         last_of_list = monster;
  213.         }
  214.         screen[monster->y][monster->x] = '*';
  215.         if (!debug_disp) {
  216.         if ((monster->y < (sy+4)) && (monster->y > (sy-4)) &&
  217.             (monster->x < (sx+6)) && (monster->x > (sx-6)))
  218.             draw_symbol((monster->x-sx+5)*3,(monster->y-sy+3)*2,'*');
  219.         } else {
  220.         draw_object(monster->y+1,monster->x+1,'*');
  221. /*
  222.         move(monster->y+1,monster->x+1);
  223.         addch('*');
  224. */
  225.         }
  226.     } else
  227.         current = monster;
  228.     move(16,0);
  229.     wrefresh(win);
  230.     }
  231.     return deadyet;
  232. }
  233.